<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\controller;

/**
 * 上传文件接口控制器
 */
class Upload extends ApiBase
{
     /**
     * 多图上传
     */
    public function uploads(){
		$data=$this->param;
		if(!empty($data['user_id'])){
			$stat=db('company')->where('user_id',$data['user_id'])->value('stat');
			if(empty($stat)){
				return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => '参数错误']);
			}
		}
		// 获取表单上传文件
		$files = request()->file('image');
		$str='';
		$arr=array();
		if($files){
			foreach($files as $file){
				if(!empty($data['user_id'])){
					$path=ROOT_PATH . 'public' . DS . 'upload' . DS .'file';
				}else{
					$path=ROOT_PATH . 'public' . DS . 'upload' . DS .'file' . DS .'trend';
				}
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info = $file->validate(['size'=>1024*1024*2,'ext'=>'jpg,png,gif,jpeg,webp'])->move($path);
				if($info){
					if(!empty($data['user_id'])){
						$str.='/upload/file/'.date('Ymd').'/'.$info->getFilename().',';
					}else{
						$str.='/upload/file/trend/'.date('Ymd').'/'.$info->getFilename().',';
					}
				}else{
					// 上传失败获取错误信息
					return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
				}    
			}
			if(!empty($data['user_id'])){
				$imgs=db('company')->where('user_id',$data['user_id'])->value('imgs');
				if(!empty($imgs)){
					$strs=$imgs.$str;
				}else{
					$strs=$str;
				}
				$arr=explode(',',substr($strs, 0, -1));
				$res=db('company')->where('user_id',$data['user_id'])->update(array('imgs'=>$strs));
			}

		}		
		if(!empty($data['user_id'])){
			return $this->apiReturn($arr);
		}else{
			return $this->apiReturn(array('str'=>$str));
		}

	}
	/**
     * 单图上传
     */
	public function upload(){
		$data=$this->param;
		$file = request()->file('image');	
		if(!empty($data['is_company'])){
			$path=ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'company';
			$s='company';
		}else{
			$path=ROOT_PATH . 'public' . DS . 'upload' . DS .'extend' . DS .'headimg';
			$s='headimg';
		}

		if($file){
			$info = $file->validate(['size'=>1024*1024*4,'ext'=>'jpg,png,gif,jpeg,webp'])->move($path);
			if($info){
				$str='/upload/extend/'.$s.'/'.date('Ymd').'/'.$info->getFilename(); 
			}else{
				// 上传失败获取错误信息
				return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => $file->getError()]);
			}
		}
		$arr=array('headimg'=>$str);
		if(!empty($data['user_id']) && empty($data['is_company'])){
			$url=db('user')->where('id',$data['user_id'])->value('headimg');
			@unlink($_SERVER['DOCUMENT_ROOT'].$url);
			db('user')->where('id',$data['user_id'])->update($arr);
		}
		return $this->apiReturn($arr);
	}
	/**
     * 删除图片
     */
    public function delimg(){
		
		$data=$this->param;
		$imgs=db('company')->where('user_id',$data['user_id'])->value('imgs');
		if(empty($imgs)){
			return $this->apiReturn([API_CODE_NAME => 1020001, API_MSG_NAME => '系统繁忙，请稍后重试']);
		}
		$arr=explode(',',substr($imgs, 0, -1));
		foreach($arr as $k=>$v){
			if($data['path'] == $v){
				unset($arr[$k]);
			} 
		}
		@unlink($_SERVER['DOCUMENT_ROOT'].$data['path']);
		$str=implode(',',$arr);
		if(count($arr) == 0){
			$sstr='';
		}else{
			$sstr=$str.',';
		}
		$res=db('company')->where('user_id',$data['user_id'])->update(array('imgs'=>$sstr));
		return $this->apiReturn();
	}
}
